Conditions | 11 |
Total Lines | 114 |
Code Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Modal.tsx ➔ Modal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import React, { createContext, useContext, useEffect, useRef } from "react"; |
||
20 | |||
21 | export default function Modal({ |
||
22 | id, |
||
23 | parentElement, |
||
24 | visible, |
||
25 | children, |
||
26 | className, |
||
27 | onModalConfirm, |
||
28 | onModalMiddle, |
||
29 | onModalCancel, |
||
30 | }: ModalProps): React.ReactPortal | null { |
||
31 | // Set up div ref to measure modal height |
||
32 | const modalRef = useRef<HTMLDivElement>(null); |
||
33 | |||
34 | const handleTabKey = (e: KeyboardEvent): void => { |
||
35 | if (modalRef && modalRef.current) { |
||
36 | const focusableModalElements = modalRef.current.querySelectorAll( |
||
37 | 'a[href], button, textarea, input[type="text"], input[type="email"], input[type="radio"], select', |
||
38 | ); |
||
39 | const firstElement = focusableModalElements[0] as HTMLElement; |
||
40 | const lastElement = focusableModalElements[ |
||
41 | focusableModalElements.length - 1 |
||
42 | ] as HTMLElement; |
||
43 | |||
44 | const focusableModalElementsArray = Array.from(focusableModalElements); |
||
45 | |||
46 | if ( |
||
47 | document.activeElement && |
||
48 | !focusableModalElementsArray.includes(document.activeElement) |
||
49 | ) { |
||
50 | firstElement.focus(); |
||
51 | e.preventDefault(); |
||
52 | } |
||
53 | |||
54 | if (!e.shiftKey && document.activeElement === lastElement) { |
||
55 | firstElement.focus(); |
||
56 | e.preventDefault(); |
||
57 | } |
||
58 | |||
59 | if (e.shiftKey && document.activeElement === firstElement) { |
||
60 | lastElement.focus(); |
||
61 | e.preventDefault(); |
||
62 | } |
||
63 | } |
||
64 | }; |
||
65 | |||
66 | // Collection of key codes and event listeners |
||
67 | const keyListenersMap = new Map([ |
||
68 | [27, onModalCancel], |
||
69 | [9, handleTabKey], |
||
70 | ]); |
||
71 | |||
72 | // Runs every time visible changes to set the overflow on the modal and update the body overflow |
||
73 | useEffect((): (() => void) => { |
||
74 | function setBodyStyle(): void { |
||
75 | document.body.style.overflow = visible ? "hidden" : "visible"; |
||
76 | } |
||
77 | setBodyStyle(); |
||
78 | // Runs on component unmount |
||
79 | return (): void => { |
||
80 | setBodyStyle(); |
||
81 | }; |
||
82 | }, [visible]); |
||
83 | |||
84 | // Adds various key commands to the modal |
||
85 | useEffect((): (() => void) => { |
||
86 | let keyListener; |
||
87 | if (visible) { |
||
88 | keyListener = (e: KeyboardEvent): void => { |
||
89 | const listener = keyListenersMap.get(e.keyCode); |
||
90 | return listener && listener(e); |
||
91 | }; |
||
92 | document.addEventListener("keydown", keyListener); |
||
93 | } |
||
94 | |||
95 | return (): void => { |
||
96 | if (keyListener !== undefined) { |
||
97 | document.removeEventListener("keydown", keyListener); |
||
98 | } |
||
99 | }; |
||
100 | }, [keyListenersMap, visible]); |
||
101 | |||
102 | if (parentElement !== null) { |
||
103 | return createPortal( |
||
104 | <div |
||
105 | aria-describedby={`${id}-description`} |
||
106 | aria-hidden={!visible} |
||
107 | aria-labelledby={`${id}-title`} |
||
108 | data-c-dialog={visible ? "active--overflowing" : ""} |
||
109 | data-c-padding="top(double) bottom(double)" |
||
110 | role="dialog" |
||
111 | ref={modalRef} |
||
112 | className={className} |
||
113 | > |
||
114 | <div data-c-background="white(100)" data-c-radius="rounded"> |
||
115 | <modalContext.Provider |
||
116 | value={{ |
||
117 | id, |
||
118 | parentElement, |
||
119 | visible, |
||
120 | onModalConfirm, |
||
121 | onModalMiddle, |
||
122 | onModalCancel, |
||
123 | }} |
||
124 | > |
||
125 | {children} |
||
126 | </modalContext.Provider> |
||
127 | </div> |
||
128 | </div>, |
||
129 | parentElement, |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | return null; |
||
134 | } |
||
216 |